<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<div class="content-section" th:fragment="content">
    <h2><i class="fas fa-procedures"></i> 办理出院 </h2>
    <div class="search-bar">
        <input type="text" placeholder="输入病人姓名搜索...">
        <button class="search-btn"><i class="fas fa-search"></i> 搜索</button>
    </div>
    <table class="patients-table">
        <thead>
            <tr>
                <th>病历号</th>
                <th>姓名</th>
                <th>性别</th>
                <th>年龄</th>
                <th>联系电话</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
</div>

<script th:inline="javascript">
$(document).ready(function() {
    // 页面加载时获取所有病人
    loadPatients();

    // 搜索按钮点击事件
    $('.search-btn').click(function() {
        loadPatients();
    });

    // 搜索框回车事件
    $('.search-bar input').keypress(function(e) {
        if(e.which == 13) {
            loadPatients();
        }
    });
});

function loadPatients() {
    var searchName = $('.search-bar input').val();

    $.get(/*[[@{/doctor/my-patients/list}]]*/ '', { searchName: searchName }, function(response) {
        if (response.code === 200) {
            var tbody = $('.patients-table tbody');
            tbody.empty();

            response.data.forEach(function(patient) {
                var row = $('<tr>');
                row.append($('<td>').text(patient.patientId));
                row.append($('<td>').text(patient.patientName));
                row.append($('<td>').text(patient.patientGender));
                row.append($('<td>').text(patient.patientAge));
                row.append($('<td>').text(patient.patientPhone || '-'));
                
                // 添加出院链接
                var dischargeLink = $('<a>', {
                    href: 'javascript:void(0)',
                    text: '办理出院',
                    class: 'discharge-link',
                    click: function() {
                        handleDischarge(patient.patientId, patient.patientName);
                    }
                });
                row.append($('<td>').append(dischargeLink));

                tbody.append(row);
            });
        } else {
            alert('获取病人列表失败：' + response.message);
        }
    }).fail(function(xhr) {
        alert('获取病人列表失败');
    });
}

// 处理出院操作
function handleDischarge(patientId, patientName) {
    if (confirm('确定为病人 ' + patientName + ' 办理出院吗？')) {
        $.ajax({
            url: /*[[@{/doctor/discharge}]]*/ '',
            type: 'POST',
            data: { patientId: patientId },
            success: function(response) {
                if (response.code === 200) {
                    alert(response.message);
                    loadPatients(); // 重新加载病人列表
                } else {
                    alert(response.message || '出院办理失败');
                }
            },
            error: function(xhr) {
                var errorMsg = '出院办理失败';
                if (xhr.responseJSON && xhr.responseJSON.message) {
                    errorMsg = xhr.responseJSON.message;
                }
                alert(errorMsg);
            }
        });
    }
}

function viewPatientDetail(patientId) {
    $.get(/*[[@{/doctor/my-patients/detail/}]]*/ '' + patientId, function(response) {
        if (response.code === 200) {
            var detail = response.data;
            var modalContent = '<div class="modal-content">' +
                '<h3>病人详细信息</h3>' +
                '<div class="detail-info">' +
                '<p><strong>病人姓名：</strong>' + detail.patientName + '</p>' +
                '<p><strong>性别：</strong>' + detail.patientGender + '</p>' +
                '<p><strong>年龄：</strong>' + detail.patientAge + '</p>' +
                '<p><strong>联系电话：</strong>' + (detail.patientPhone || '-') + '</p>' +
                '<p><strong>住院信息：</strong>' + detail.wardInfo.floorNum + '楼 ' +
                detail.wardInfo.wardNum + '号房 ' + detail.wardInfo.bedNum + '号床</p>' +
                '<p><strong>病情描述：</strong>' + (detail.conditionDesc || '暂无描述') + '</p>' +
                '<p><strong>入住时间：</strong>' + (detail.beginTime || '暂无记录') + '</p>' +
                '</div></div>';

            showModal(modalContent);
        } else {
            alert('获取病人详情失败：' + response.message);
        }
    }).fail(function(xhr) {
        alert('获取病人详情失败');
    });
}

</script>

<style>
/* 表格样式优化 */
.patients-table {
    margin-top: 20px;
    width: 100%;
    border-collapse: collapse;
}

.patients-table th,
.patients-table td {
    padding: 12px;
    text-align: left;
    border-bottom: 1px solid #e0e0e0;
}

.patients-table th {
    background-color: #f5f5f5;
    font-weight: 600;
}

.patients-table tr:hover {
    background-color: #f8f9fa;
}

/* 搜索栏样式优化 */
.search-bar {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
}

.search-bar input {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 14px;
}

.search-bar input:focus {
    outline: none;
    border-color: #4CAF50;
    box-shadow: 0 0 0 2px rgba(76, 175, 80, 0.2);
}

.search-btn {
    padding: 8px 16px;
    background-color: #4CAF50;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 5px;
}

.search-btn:hover {
    background-color: #45a049;
}

/* 添加出院链接样式 */
.discharge-link {
    color: #dc3545;
    text-decoration: none;
    cursor: pointer;
}

.discharge-link:hover {
    text-decoration: underline;
    color: #c82333;
}
</style>
</body>
</html> 